package xcmg.device.service.wechat;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.area.DealerStorefrontDO;
import xcmg.device.dao.mapper.DealerStorefrontMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.LocationUtil;
import xcmg.device.service.vo.screen.ScreenNetWorkVO;
import xcmg.device.service.vo.wechat.DealerStorefrontVO;
import xcmg.device.service.vo.wechat.StoreSearchParam;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class StorefrontService {

    @Autowired
    private DealerStorefrontMapper dealerStorefrontMapper;

    @Autowired
    private FastGenClient fastGenClient;

    public ErrorCode addStore(DealerStorefrontVO dealerStorefrontVO) throws IOException {
        DealerStorefrontDO dealerStorefrontDO=new DealerStorefrontDO();
        BeanUtils.copyProperties(dealerStorefrontVO,dealerStorefrontDO);
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode()) {
            return ErrorCode.Failure;
        }
        String storeId = textResult.getValue();
        dealerStorefrontDO.setStoreId(storeId);
        dealerStorefrontDO.setCreateDate(new Date());
        dealerStorefrontDO.setUpdateDate(new Date());
        dealerStorefrontDO.setIsDel(0);
        String address=dealerStorefrontDO.getProvince()+dealerStorefrontDO.getCity()+dealerStorefrontDO.getArea()+dealerStorefrontDO.getAddreass();
        Object[] o=LocationUtil.getCoordinate(address);
        if(o!=null){
            try{
                BigDecimal longitude=new BigDecimal(o[0].toString().trim());
                dealerStorefrontDO.setLongitude(longitude);
                BigDecimal latitude=new BigDecimal(o[1].toString().trim());
                dealerStorefrontDO.setLatitude(latitude);
            }catch (Exception e){
                return ErrorCode.GetCoordinateError;
            }
        }
        return dealerStorefrontMapper.insert(dealerStorefrontDO)>0?ErrorCode.Success:ErrorCode.Failure;
    }

    public PageCommonVO list(SearchCommonVO<StoreSearchParam> condition){
        PageCommonVO<DealerStorefrontVO> result = new PageCommonVO<>();
        List<DealerStorefrontVO> voList = new ArrayList<>();
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize(), "create_date desc");
        List<DealerStorefrontDO> doList = dealerStorefrontMapper.list(condition.getFilters());
        for (DealerStorefrontDO dealerStorefrontDO:doList
             ) {
            DealerStorefrontVO dealerStorefrontVO=new DealerStorefrontVO();
            BeanUtils.copyProperties(dealerStorefrontDO,dealerStorefrontVO);
            voList.add(dealerStorefrontVO);
        }
        result.setPageInfo(new PageInfo(doList));
        result.setPageInfoList(voList);
        return result;
    }

    public DealerStorefrontVO item(String storeId){
        DealerStorefrontDO dealerStorefrontDO=dealerStorefrontMapper.item(storeId);
        DealerStorefrontVO dealerStorefrontVO=new DealerStorefrontVO();
        BeanUtils.copyProperties(dealerStorefrontDO,dealerStorefrontVO);
        return dealerStorefrontVO;
    }

    public ErrorCode update(DealerStorefrontVO dealerStorefrontVO) throws IOException {
        DealerStorefrontDO dealerStorefrontDO=dealerStorefrontMapper.item(dealerStorefrontVO.getStoreId());
        if(dealerStorefrontDO==null){
            return  ErrorCode.StoreNotExist;
        }
        dealerStorefrontDO.setProvinceId(dealerStorefrontVO.getProvinceId());
        dealerStorefrontDO.setProvince(dealerStorefrontVO.getProvince());
        dealerStorefrontDO.setCityId(dealerStorefrontVO.getCityId());
        dealerStorefrontDO.setCity(dealerStorefrontVO.getCity());
        dealerStorefrontDO.setAreaId(dealerStorefrontVO.getAreaId());
        dealerStorefrontDO.setArea(dealerStorefrontVO.getArea());
        dealerStorefrontDO.setAddreass(dealerStorefrontVO.getAddreass());
        dealerStorefrontDO.setContactName(dealerStorefrontVO.getContactName());
        dealerStorefrontDO.setContactTel(dealerStorefrontVO.getContactTel());
        dealerStorefrontDO.setIsEnable(dealerStorefrontVO.getIsEnable());
        dealerStorefrontDO.setStoreName(dealerStorefrontVO.getStoreName());
        dealerStorefrontDO.setUpdateId(dealerStorefrontVO.getUpdateId());
        dealerStorefrontDO.setUpdateDate(new Date());
        String address=dealerStorefrontDO.getProvince()+dealerStorefrontDO.getCity()+dealerStorefrontDO.getArea()+dealerStorefrontDO.getAddreass();
        Object[] o=LocationUtil.getCoordinate(address);
        if(o!=null){
            try{
                BigDecimal longitude=new BigDecimal(o[0].toString().trim());
                dealerStorefrontDO.setLongitude(longitude);
                BigDecimal latitude=new BigDecimal(o[1].toString().trim());
                dealerStorefrontDO.setLatitude(latitude);
            }catch (Exception e){
                return ErrorCode.GetCoordinateError;
            }
        }
        return dealerStorefrontMapper.update(dealerStorefrontDO)>0?ErrorCode.Success:ErrorCode.Failure;
    }

    public ErrorCode delete(DealerStorefrontVO dealerStorefrontVO){
        DealerStorefrontDO dealerStorefrontDO=dealerStorefrontMapper.item(dealerStorefrontVO.getStoreId());
        if(dealerStorefrontDO==null){
            return  ErrorCode.StoreNotExist;
        }
        return dealerStorefrontMapper.delete(dealerStorefrontVO.getStoreId())>0?ErrorCode.Success:ErrorCode.Failure;
    }


  /**
   * 网点信息同步
   *
   * @param orgId
   * @return
   */
  public List<DealerStorefrontVO> list4Sync(String orgId) {
    List<DealerStorefrontDO> doList = dealerStorefrontMapper.list4Sync(orgId);
    if (CollectionUtils.isEmpty(doList)) {
      return new ArrayList<>();
    }
    List<DealerStorefrontVO> voList = new ArrayList<>();
    DealerStorefrontVO vo;
    for (DealerStorefrontDO storefrontDO : doList) {
      vo = new DealerStorefrontVO();
      BeanUtils.copyProperties(storefrontDO, vo);
      voList.add(vo);
    }
    return voList;
  }

  public List<ScreenNetWorkVO> getAllNetWork(){
      List<ScreenNetWorkVO> result = new LinkedList<>();
      List<DealerStorefrontDO> dealerStorefronts = dealerStorefrontMapper.selectScreenNetWorks();
      int id = 0;
      for(DealerStorefrontDO dealerStorefront : dealerStorefronts){
          ScreenNetWorkVO screenNetWorkVO = new ScreenNetWorkVO();
          String dealerOrgId = dealerStorefront.getDealerOrgId();
          String storeName = dealerStorefront.getStoreName();
          BigDecimal longitude = dealerStorefront.getLongitude();
          BigDecimal latitude = dealerStorefront.getLatitude();
          screenNetWorkVO.setDealerOrgId(dealerOrgId);
          screenNetWorkVO.setName(storeName);
          screenNetWorkVO.setLng(longitude);
          screenNetWorkVO.setLat(latitude);
          screenNetWorkVO.setId(++id);
          result.add(screenNetWorkVO);
      }
      return result;
  }
}
